//
//  UIColor+XMExtension.swift
//  AnneKit-Swift
//
//  Created by 寜小陌 on 2021/3/3.
//  Copyright © 2021 宁小陌. All rights reserved.
//

import Foundation
import UIKit

/// 颜色
extension UIColor {
    /// rgb hex 0x000000
    class func UIColorFromRGB(rgbValue: UInt32) -> UIColor {
        let temp = UInt32(255.0)
        let red = ((rgbValue & 0xFF0000) >> 16) / temp
        let green = ((rgbValue & 0xFF00) >> 8) / temp
        let blue = (rgbValue & 0xFF) / temp
        return UIColor(red: CGFloat(red), green: CGFloat(green), blue: CGFloat(blue), alpha: 1)
    }
    /// rgb 值
    class func kColorWithRGB(red:Float,green:Float,blue:Float) -> UIColor {
        return kColorWithRGBA(red: red, green: green, blue: blue, alpha: 1.0)
    }

    /// rgba 值
    class func kColorWithRGBA(red:Float,green:Float,blue:Float,alpha:Float) -> UIColor {
        return UIColor.init(_colorLiteralRed: red/255.0, green: green/255.0, blue: blue/255.0, alpha: alpha)
    }

    /// 随机颜色
    class func kRandomColor()->UIColor{
        let red = CGFloat(arc4random()%256)/255.0
        let green = CGFloat(arc4random()%256)/255.0
        let blue = CGFloat(arc4random()%256)/255.0
        print("red:\(red),green:\(green),blue:\(blue)")
        return UIColor(red: red, green: green, blue: blue, alpha: 1.0)
    }
}

extension UIColor {
    /// 通过颜色生成图片
    public func nxm_makeImage() -> UIImage {
        let rect = CGRect.init(x: 0.0, y: 0.0, width: 6.0, height: 6.0)
        UIGraphicsBeginImageContext(rect.size)
        let context : CGContext = UIGraphicsGetCurrentContext()!
        context.setFillColor(self.cgColor)
        context.fill(rect)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image!
    }
}

/// 渐变色
extension CAGradientLayer {
    /// 渐变方向
    public enum CAGradientDirection {
        case vertical
        case horizontal
    }
    
    /**
        使用说明 一定先要设置frame
        let gradientLayer = CAGradientLayer()
        gradientLayer.frame = CGRect.init(x: 0, y: 0, width: 200, height: 30)
        let cgColors = [UIColor.white.cgColor,UIColor.red.cgColor]
        gradientLayer = gradientLayer.jkMakeLayer(cgColors: cgColors, direction: .horizontal)
        self.layer.insertSublayer(gradientLayer, at: 0)
     */
    
    /**
     * 参数说明
     * cgColors CGColor类型数组
     * direction CAGradientDirection枚举，表示颜色渐变方向
     * locations 每个颜色对应的位置，区间在[0-1]之间.比如三种颜色的集合[A,B,C],设置的区间为[0, 0.2, 1]
     那么A和B均分前0~0.2区间的位置颜色，B和C均分[0.2~1]区间的位置颜色
     */
    public func nxm_makeLayer(colors: [UIColor], direction: CAGradientDirection = .horizontal, locations: [NSNumber] = []) {
        
        let cgColors = colors.map {
            $0.cgColor
        }
        
        var gradientLocations:[NSNumber] = locations
        
        if locations.isEmpty {
            let arrayM = NSMutableArray()
            let spacing = 1 / Double((cgColors.count - 1))
            
            for i in 0..<cgColors.count {
                let location = Double(i) * spacing
                arrayM.add(NSNumber.init(value: location))
            }
            gradientLocations = arrayM as! [NSNumber]
        }
        
        
        //创建CAGradientLayer对象并设置参数
        self.colors = cgColors
        self.locations = gradientLocations
        
        //设置渲染的起始结束位置（横向渐变）
        if direction == .horizontal {
            self.startPoint = CGPoint(x: 0, y: 0)
            self.endPoint = CGPoint(x: 1, y: 0)
        } else {
            self.startPoint = CGPoint(x: 0, y: 0)
            self.endPoint = CGPoint(x: 0, y: 1)
        }
    }
}


